Archetype.TemplatesLoaded = $H();

Archetype.Templates = {
	AbstractTemplate: Archetype.Class.create({
		initialize: _,
		evaluate: _
	})
}

Object.extend(Archetype.Templates, {
	require: function(/*Array*/ modules, /*function*/ callback, /*Constructor*/ templateSystem) {
		Archetype.require(modules, {
	        evaluation: "html",
	        callback: this._onTemplatesLoaded.bind(this, modules, callback, templateSystem)
	    });
	},
	_onTemplatesLoaded: function(/*Array*/ modules, /*function*/ callback, /*Constructor*/ templateSystem, /*HashMap*/ templates) {
		$A(modules).each(function(module) {
			if(Archetype.TemplatesLoaded.get(module) == null) {
				var url = $U(module, "html");
				var content = $H(templates).get(url);
				var template = new templateSystem(content);
				Archetype.TemplatesLoaded.set(module, template);
			}
		}.bind(this));

		callback();
	},
	add: function(/*String*/ module, /*String*/ content, /*String*/ templateSystem) {
		if(typeof templateSystem == "undefined") {
			templateSystem = Archetype.template;
		}
		var templateSystemWithUpperCase = templateSystem.substr(0, 1).toUpperCase() + templateSystem.substr(1) + "Template";
		if(typeof Archetype.Templates[templateSystemWithUpperCase] != "function") {
			Archetype.TemplatesLoaded.set(module, {
				content: content,
				templateSystem: templateSystem
			});
		} else {
			var template = new Archetype.Templates[templateSystemWithUpperCase](content);
			Archetype.TemplatesLoaded.set(module, template);
		}
	},
	get: function(/*String*/ module) {
		var template = Archetype.TemplatesLoaded.get(module);
		if(typeof template.content == "string") {
			this.add(module, template.content, template.templateSystem);
			return Archetype.TemplatesLoaded.get(module);
		} else {
			return template;
		}
	}
});
















